<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * CMPP服务器模型
 * @package app\admin\model
 */
class CmppService extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_cmpp_client_reg_center';
    protected $observerClass = 'app\admin\event\DeleteCache';
    /**
     * CMPP服务器
     */
    public function getCmppAll($where=[])
    {
    	$data = [];
    	if(session('user_type')=='admin')
    	{
    		$data = $this
            ->field('*')
            ->where($where)
            ->paginate(15);
    	}
    	return $data;
    }

    public function getClientAlls($request)
    {
        $data = [];
        if(session('user_type')=='admin') {

            $data = $this->field('*')->paginate($request['limit'], false, $request)
            ->each(function($item, $key) {

                switch ($item->onoff) {

                    case 1:
                        $item->onoff_ = '<span class="layui-badge layui-bg-green status-qy">启用</span>';
                        break;

                    default:
                        $item->onoff_ = '<span class="layui-badge status-jy">禁用</span>';
                        break;
                }
            });
        }

        return $data;
    }

    /**
     * 添加
     */
    public function addDataOne($data)
    {
        $data['createtime'] = date('Y-m_d H:i:s');
        $result = $this->save($data);

        if ($result !== false)
        {
            trace('用户'.session('username').'添加CMPP服务器:' . json_encode($data,JSON_UNESCAPED_UNICODE),'info');
            return ['code' => 1, 'msg' => '添加成功'];
        }
        else
        {
            return ['code' => 0, 'msg' => '添加失败'];
        }
    }

    /**
     * 编辑
     */
    public function updateDataOne($data)
    {
        $result = $this->save($data, ['id' => $data['id']]);

        if ($result !== false)
        {
            trace('用户'.session('username').'编辑CMPP服务器:'.json_encode($data,JSON_UNESCAPED_UNICODE),'info');
            return ['code' => 1, 'msg' => '编辑成功'];
        }
        else
        {
            return ['code' => 0, 'msg' => '编辑失败'];
        }
    }

}